package cn.edu.ncu.housinghub.config;

import cn.edu.ncu.housinghub.interceptor.AgentRoleCheckInterceptor;
import cn.edu.ncu.housinghub.interceptor.IdentityVerificationInterceptor;
import cn.edu.ncu.housinghub.interceptor.LandlordRoleInterceptor;
import cn.edu.ncu.housinghub.interceptor.UserRoleCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private IdentityVerificationInterceptor identityVerificationInterceptor;
    @Autowired
    private LandlordRoleInterceptor landlordRoleInterceptor;
    @Autowired
    private AgentRoleCheckInterceptor agentRoleCheckInterceptor;
    @Autowired
    private UserRoleCheckInterceptor userRoleCheckInterceptor;
    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册登录拦截器，拦截所有/housinghub/开头的请求，但排除登录和注册接口和管理员接口
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login", "/user/register", "/user/send-code", "/admin/**", "/test/**", "/oss/**", "/upload/**", "/ws/**")
                .order(1);

        // 注册管理员登录拦截器 ，拦截所有/admin/开头的请求，但排除登录接口
        registry.addInterceptor(adminLoginInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .order(1);
        // 注册实名验证拦截器，拦截所有landlord和agent开头的请求，排除登录和注册接口
        registry.addInterceptor(identityVerificationInterceptor)
                .addPathPatterns("/landlord/**","/agent/**")
                .excludePathPatterns("/landlord/findPropertyVerificationByUserId",
                        "/landlord/findHouseByUserId",
                        "/landlord/getPropertyVerificationByUserId")
                .order(2);

        // 注册房东角色拦截器，拦截所有landlord开头的请求，排除登录和注册接口
        registry.addInterceptor(landlordRoleInterceptor)
                .addPathPatterns("/landlord/**")
                .excludePathPatterns("/landlord/submitVerification",
                        "/landlord/getPropertyVerificationByUserId")
                .order(3);

        // 注册中介角色拦截器，拦截所有agent开头的请求，排除用户申请中介
        registry.addInterceptor(agentRoleCheckInterceptor).addPathPatterns("/agent-rental/**",
                        "/agent/**")
                .excludePathPatterns("/agent/auth/apply")
                .order(3);

        // 注册用户角色校验拦截器，用于校验特定操作的用户角色
        registry.addInterceptor(userRoleCheckInterceptor)
                .addPathPatterns("/**")
                .order(4);
    }

    // CORS配置已移至CorsConfig.java，避免配置冲突
    // @Override
    // public void addCorsMappings(CorsRegistry registry) {
    //     registry.addMapping("/**")
    //             .allowedOrigins("http://localhost:5177", "http://localhost:5175", "http://localhost:5174", "http://localhost:5173", "http://localhost:3000", "http://127.0.0.1:5177")
    //             .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
    //             .allowedHeaders("Content-Type", "Authorization", "info", "X-Requested-With", "Accept", "Origin")
    //             .allowCredentials(true)
    //             .maxAge(3600);
    // }
}
